﻿using Aspose.Words.Tables;
using Aspose.Words;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Aspose.Words.Saving;
using BasicInformationCommon.API.BasicInformationCommons.Suppliers;
using ERPSystem.Common.DTO;
using MediatR;
using System.Threading;
using ERPSystem.Common.Domains.supplier;
using Mysqlx.Crud;

namespace BasicInformationCommon.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PrintController : ControllerBase
    {

        private readonly IMediator _mediator;

        public PrintController(IMediator mediator)
        {
            _mediator = mediator;
        }

        


        [HttpGet("GetPDF")]
        public async Task<IActionResult> GetPDF(int id)
        {
           var data=await  PurchaseOrderFindHandle(new PurchaseOrderFindCommand() { PurchaseOrderId= id });
            var purchaseOrderData = data.Data;
            var document = await CreatePDF(purchaseOrderData);
            var saveOptions = new PdfSaveOptions { SaveFormat = SaveFormat.Pdf, OpenHyperlinksInNewWindow = true };
            var docStream = new MemoryStream();
            document.Save(docStream, saveOptions);

            // 将MemoryStream转换为字节数组
            byte[] pdfBytes = docStream.ToArray();

            // 返回PDF文件作为HTTP响应
            return File(pdfBytes, "application/pdf", "generated_document.pdf");


        }


        /// <summary>
        /// 采购订单反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result<PurchaseOrder>> PurchaseOrderFindHandle([FromQuery] PurchaseOrderFindCommand request)
        {
           
            return await _mediator.Send(request);

        }



        private async Task<Document> CreatePDF(PurchaseOrder purchaseOrderData)
        {
            var document = new Document();
            var builder = new DocumentBuilder(document)
            {
                PageSetup = {
                    PageWidth = ConvertUtil.MillimeterToPoint(120),
                    PageHeight = ConvertUtil.MillimeterToPoint(300),
                    Orientation = Orientation.Landscape,
                    TopMargin = ConvertUtil.MillimeterToPoint(4),
                    BottomMargin = ConvertUtil.MillimeterToPoint(4),
                    LeftMargin = ConvertUtil.MillimeterToPoint(4),
                    RightMargin = ConvertUtil.MillimeterToPoint(4)
                }
            };
            DocumentTitle(builder, "翻斗花园公司");
            DocumentHead(builder, purchaseOrderData);
            ProductProcessHead(builder);
            ProductProcess(builder, purchaseOrderData);
            SignProcess(builder);
            return await Task.FromResult(document);
        }
        /// <summary>
        /// 标题
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="title"></param>
        private void DocumentTitle(DocumentBuilder builder, string title)
        {
            builder.CellFormat.ClearFormatting();
            builder.Font.Size = 16;
            builder.Font.NameFarEast = "黑体";
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.Writeln(title + " 订单");
            builder.Font.Size = 12;
            builder.Font.NameFarEast = "黑体";
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        }
        /// <summary>
        /// 表头
        /// </summary>
        /// <param name="builder"></param>
        private void DocumentHead(DocumentBuilder builder, PurchaseOrder purchaseOrderData)
        {


            builder.Font.Size = 12;
            builder.StartTable();
            builder.CellFormat.ClearFormatting();
            builder.CellFormat.Borders.LineStyle = LineStyle.None;
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.TopPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.CellFormat.FitText = true;

            builder.InsertCell();
            builder.Write("单据编码:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.InsertCell();
            builder.Write(purchaseOrderData.ReceiptNum);
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(48);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);


            builder.InsertCell();
            builder.Write("");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);




            builder.InsertCell();
            builder.Write("打印时间:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);
            builder.InsertCell();
            builder.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(48);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);

            builder.EndRow();


            builder.InsertCell();
            builder.Write("供应商:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(1);
            builder.InsertCell();
            builder.Write(purchaseOrderData.System);
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);



            builder.InsertCell();
            builder.Write("");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);



            builder.InsertCell();
            builder.Write("单据状态:");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(26);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);
            builder.InsertCell();
            builder.Write("审核");
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(48);
            builder.CellFormat.LeftPadding = Aspose.Words.ConvertUtil.MillimeterToPoint(0);






            builder.EndRow();

            builder.EndTable();
        }
        /// <summary>
        /// 表体表格的 表头
        /// </summary>
        /// <param name="builder"></param>
        private void ProductProcessHead(DocumentBuilder builder)
        {
            var productTable = builder.StartTable();
            productTable.SetBorders(LineStyle.None, 0d, System.Drawing.Color.White);
            builder.CellFormat.Borders.LineStyle = LineStyle.Single;
            builder.Font.NameFarEast = "黑体";
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
            builder.Font.Size = 12;


            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write("物料编码");

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write("物料名称");

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write("型号规格");

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write("主计量");


            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write("数量");


            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write("入库数量");




            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write("到货说明");


            builder.EndRow();



            builder.EndTable();

        }
        /// <summary>
        /// 表格内容
        /// </summary>
        /// <param name="builder"></param>
        private void ProductProcess(DocumentBuilder builder, PurchaseOrder purchaseOrderData)
        {
           

            builder.StartTable();
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
            builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write(purchaseOrderData.MaterialCode);

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write(purchaseOrderData.MaterialName);

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write(purchaseOrderData.TypeSpecification);

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write(purchaseOrderData.DocumentType);


            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write(purchaseOrderData.Quantity.ToString());

            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);
            builder.Write(purchaseOrderData.ArrivalQuantity.ToString());


            builder.InsertCell();
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            builder.Write(purchaseOrderData.PurchaseDescription);

            builder.EndRow();

            

            //builder.InsertCell();
            //builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            //builder.Write("扣减");

            //builder.InsertCell();
            //builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            //builder.Write("黑猪,残疾");

            //builder.EndRow();

            //builder.InsertCell();
            //builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(50);
            //builder.Write("备注");

            //builder.InsertCell();
            //builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(150);
            //builder.Write("啦啦啦");


            builder.EndTable();
        }
        /// <summary>
        /// 页脚
        /// </summary>
        /// <param name="builder"></param>
        private void SignProcess(DocumentBuilder builder)
        {
            var signTable = builder.StartTable();
            builder.Font.Size = 12;
            builder.CellFormat.Borders.LineStyle = LineStyle.None;
            builder.RowFormat.Height = Aspose.Words.ConvertUtil.MillimeterToPoint(4);
            builder.InsertCell();
            builder.Write($"制单人：");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(20);
            builder.InsertCell();
            builder.Write("liy");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);

            builder.InsertCell();
            builder.Write("");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);

            builder.InsertCell();
            builder.Write("审核人:");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(60);
            builder.InsertCell();
            builder.Write("");
            builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
            builder.CellFormat.Width = Aspose.Words.ConvertUtil.MillimeterToPoint(30);

            builder.EndRow();
            builder.EndTable();
        }

    }
}
